<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <link rel="stylesheet" type="text/css" th:href="@{/component/dhtmlxSuite/skins/skyblue/dhtmlx.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/component/dhtmlxSuite/codebase/dhtmlx.css}}]]
    <link rel="stylesheet" type="text/css" th:href="@{/component/dhtmlxSuite/codebase/fonts/font_roboto/roboto.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/resources/css/main.css}"/>
    <script th:src="@{/component/dhtmlxSuite/codebase/dhtmlx.js}"></script>
    <script language="javascript" th:src="@{/resources/js/jquery.js}"></script>
    <script type="text/javascript" th:src="@{/resources/js/jqueryui.js}"></script>
    <script language="javascript" th:src="@{/resources/js/Array.js}"></script>
    <script language="javascript" th:src="@{/resources/js/control.js}"></script>
    <script th:src="@{/resources/js/validate.js}"></script>
    <style type="text/css">
        div#layoutObj {
            position: relative;
            margin-top: 10px;
            margin-left: 10px;
            margin-right:10px;
            margin-bottom:10px;
            width: 95%;
            height: 95%;
        }
        html, body {
            width: 100%;
            height: 100%;
            margin: 10px;
            overflow: hidden;
        }

    </style>
</head>
<body>
<script language="javascript" th:src="@{resources/js/crud.js}"></script>
<script type="text/javascript">
    dhtmlx.message.defPosition="bottom";
    var ctx = "[[${#httpServletRequest.getContextPath()}]]/";
    var queryUrl=ctx+"system/menu/list";

    var dhxLayout = new dhtmlXLayoutObject(document.body, "1C");
    var dhxToobar = dhxLayout.cells("a").attachToolbar();
    dhxLayout.cells("a").hideHeader();
    dhxToobar.setIconsPath(ctx+"component/dhtmlxSuite/comm/imgs/");
    dhxToobar.addButton("new", 0, '[[#{btn.add}]]', "new.gif", "new_dis.gif");
    dhxToobar.addButton("edit", 1, '[[#{btn.modi}]]',"open.gif", "open_dis.gif");
    dhxToobar.addButton("delete", 2, '[[#{btn.delete}]]',"close.gif", "close_dis.gif");
    dhxToobar.addButton("assign", 3, '[[#{btn.add}]]',"open.gif", "open_dis.gif");
    var dhxTree=dhxLayout.cells("a").attachTree();
    dhxTree.setImagePath(ctx+"component/dhtmlxSuite/codebase/imgs/dhxtree_material/");
    dhxTree.enableTreeLines(true);
    dhxTree.setImageArrays("plus", "plus2.gif", "plus3.gif", "plus4.gif", "plus.gif", "plus5.gif");
    dhxTree.setImageArrays("minus", "minus2.gif", "minus3.gif", "minus4.gif", "minus.gif", "minus5.gif");
    dhxTree.setStdImages("leaf.gif", "folderOpen.gif", "folderClosed.gif");
    dhxTree.setDataMode("json");

    dhxTree.load(ctx+"system/menu/list","json");
    dhxToobar.attachEvent("onClick", function(id){
        if(id=='new'){
            goAdd();
        }else if(id=='edit'){
            goEdit();
        }else if(id=='delete'){
            goDelete();
        }else if(id=='assign'){
            goAssign();
        }
    });


    var editFormContent=[
        {type:"settings", position:"label-left",lableWidth: 100,inputWidth: 120},
        {type: "fieldset", label: "菜单信息",offsetLeft:10, inputWidth: 520, lableWidth: 100,list:[
                {type: "hidden", name:"id", value:""},
                {type: "hidden", name:"parentId", value:""},
                {type:"input", name:"resName", label:"菜单名:",validate:"NotEmpty"},
                {type:"input", name:"resCode", label:"编码:",validate:"NotEmpty"},
                {type:"newcolumn",offset:20},
                {type:"input", name:"actionUrl", label:"URL:",validate:"NotEmpty"},
                {type:"input", name:"orderNo", label:"序号:",validate:"NotEmpty"}
            ]},
        {type: "block", inputWidth: 510, list: [
                {type: "settings", offsetTop: 10},
                {type:"button", name:"cmdOK",width:30, value:"确定",offsetLeft: 150},
                {type: "newcolumn"},
                {type:"button", name:"cmdCancel",width:30, value:"取消"}
            ]}
    ];
    function addInit(form){
        form.attachEvent("onButtonClick", function(name, command){
            form.validate();
            if(name=="cmdOK"){
                var pid=this.getItemValue("parentId");
                this.send(ctx+"system/menu/save",function(loader, response){
                    var tobj= eval('(' + response + ')');
                    if(tobj.success=='true'){
                        dhtmlx.message({
                            text: "[[#{message.saveSuccess}]]",
                            expire: 10
                    });
                        var menu=tobj.menu;
                        dhxTree.insertNewChild(pid,menu.id,menu.resName,0,0,0,0,"CHILD");
                        closedialog(true);
                    }else{
                        openMsgDialog("[[#{message.saveFailed}]]","[[#{message.errorMsg}]]"+tobj.message,300,200);
                    }
                });
            }else if(name=='cmdCancel'){
                closedialog(false);
            }
        });
    }
    function goAdd(){
        var id=dhxTree.getSelectedItemId();
        if(id==''){
            openMsgDialog("添加菜单","请选择上级菜单",300,200);
        }else{
            var form=openWindowForAdd("添加菜单",editFormContent,550,250,addInit);
            form.setItemValue("parentId",id);
        }
    }
    function editInit(form){
        form.attachEvent("onButtonClick", function(name, command){
            if(name=="cmdOK"){
                this.send(ctx+"system/menu/update",function(loader, response){
                    var tobj= eval('(' + response + ')');
                    if(tobj.success=='true'){
                        dhtmlx.message({
                            text: "[[#{title.saveSuccess}]]",
                            expire: 10
                        });
                        closedialog(true);
                        reload();
                    }else{
                        openMsgDialog("[[#{title.saveFailed}]]","[[#{message.errorMsg}]]"+tobj.message,300,200);
                    }
                });
            }else if(name=='cmdCancel'){
                closedialog(false);
            }
        });
    }
    function goEdit(){
        var id=dhxTree.getSelectedItemId();
        if(id==''){
            openMsgDialog("编辑菜单","[[#{message.alertSelectAtLeastOneRow}]]",300,150);
        }else if(id.indexOf(",")!=-1){
            openMsgDialog("编辑菜单","[[#{message.alertSelectMutilRow}]]",300,150);
        }else{
            var form=openWindowForEdit("修改菜单",editFormContent,550,250,editInit);
            //form.load(ctx+"system/sysuser/edit?id="+list);
            $.ajax({
                type:"get",
                url:ctx+"system/menu/edit?id="+id,
                dataType: "json",
                success:function(data){
                    var obj=eval(data);
                    form.setItemValue("id",obj.model.id);
                    form.setItemValue("resName",obj.model.resName);
                    form.setItemValue("resCode",obj.model.resCode);
                    form.setItemValue("actionUrl",obj.model.actionUrl);
                    form.setItemValue("orderNo",obj.model.orderNo);
                }
            });
        }
    }
    function goAssign(){
        var id=dhxTree.getSelectedItemId();
        if(id==''){
            openMsgDialog("菜单赋权","请选择菜单",300,150);
        }else if(id.indexOf(",")!=-1){
            openMsgDialog("菜单赋权","[[#{message.alertSelectMutilRow}]]",300,150);
        }else{
            var form=openWindow("菜单角色赋权",ctx+"system/menu/showrole?id="+id,600,450);
        }
    }

</script>

</body>
</html>